(function(S) {
	var querySeparator = {
		// 匹配单词的部分包含testValue的语句
		'*=' : function(value, testValue) {
			return new RegExp('^.*' + testValue + '.*$').test(value);
		},
		// 匹配单词的开始包含testValue的语句
		'^=' : function(value, testValue) {
			return new RegExp('^' + testValue + '.*').test(value);
		},
		// 匹配单词的结束包含testValue的语句
		'$=' : function(value, testValue) {
			return new RegExp('.*' + testValue + '$').test(value);
		},
		// 匹配包含testValue的语句，应该有空格隔开
		'~=' : function(value, testValue) {
			return new RegExp('\s*' + testValue + '\s*').test(value);
		},
		'=' : function(value, testValue) {
			if (!testValue && value) {
				return true;
			}

			return value == testValue;
		},
		'!=' : function(value, testValue) {
			return value != testValue;
		},
		// 匹配包含testValue的语句，应该有-隔开
		'|=' : function(value, testValue) {
			return new RegExp('-*' + testValue + '-*').test(value);
		}
	};
	/**
	 * @class S.JsonStore
	 */
	S.declare('JsonLocalStore', S.EventTarget, {

		data : null,

		identifier : 'id',

		index : null,

		init : function() {
			this.setData(this.data);
		},
		setData : function(data) {
			if (data.items) {
				this.identifier = data.identifier;
				this.data = data.items;
			}
			this.index = {};
			S.each(this.data, function(item, index) {
				this.index[item[this.identifier]] = index;
			}, this);
			this.trigger('set', {
				data : this.data
			});
		},
		get : function(id) {
			var item = this.data[this.index[id]];
			return item;
		},
		getIdentity : function(item) {
			return item[this.identifier];
		},
		put : function(item, config) {
			var id, index = this.index, data = this.data, identifier = this.identifier;

			id = item.id || (S.uuid++);

			item.id = id;

			if (config && config.id) {
				id = config.id;
			}

			if ( id in index) {
				if (config && config.override === false) {
					throw new Error('item already exsits!');
				} else {
					data[index[id]] = item;
				}
			} else {
				index[id] = data.push(item) - 1;
			}
			this.trigger('put', {
				data : item
			});
			return id;
		},
		add : function(item, config) {
			config = config || {};
			config.override = false;
			return this.put(item, config);
		},
		remove : function(id) {
			var index = this.index, data = this.data;
			if ( id in index) {
				this.trigger('remove', {
					data : data[index[id]]
				});
				data.splice(index[id], 1);
				this.setData(data);
				return true;
			} else {
				return false;
			}
		},
		find : function(query) {
			var result = [], queryString, start, count;
			if ( typeof query === 'function') {
				S.each(this.data, function(item, index) {
					if (query.call(item, item, index)) {
						result.push(item);
					}
				});

				return result;
			}
			if (S.isPlainObject(query)) {
				queryString = query.query;
				start = query.start || 0;
				count = query.count || 0;
			} else if ( typeof query === 'string') {
				queryString = query;
				start = 0;
				count = 0;
			}
			var queryArr = queryString.split(',');
			S.each(this.data, function(item, index) {
				var isMatch = true;

				S.each(queryArr, function(queryItem) {
					var match = queryItem.match(/^([\w\s-]*)([\*~\$\^\!]?=)[\"\']?([\w\s-]*)[\"\']?$/);
					if (match[2]) {

						if (!querySeparator[match[2]](item[match[1]], match[3])) {
							isMatch = false;

							return false;
						}
					}
				});

				if (isMatch) {
					result.push(item);
				}
			});

			if (count) {
				result.slice(start, count);
			}

			this.trigger('find', {
				data : result
			});
			return result;
		}
	});

})(Simple);
